﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using ConsoleApp.MyBatis;
using ConsoleApp.Dapper;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Data;
using ConsoleApp.StructureMap;
using ConsoleApp.Openxml;
using System.Net.Sockets;
using System.Net.Http;
using System.Net.Http.Headers;

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            //demo_rx.demo();

            //demo_modify_config_file.demo();

            //demo_A a = new demo_A { Name = "bob" };
            //demo_A b = a;
            //a = null;

            //Func<int, int> fac = null;
            //fac = x => x <= 1 ? 1 : x * fac(x - 1);
            //Console.WriteLine("修改前fac:" + fac.Invoke(5));  //print 120
            //Func<int, int> facAlias = fac;
            //fac = x => x;
            //Console.WriteLine("facAlias:" + facAlias(5));    //print 20
            //Console.WriteLine("修改后fac:" + fac.Invoke(5));  //print 5

            //demo_struct.demo();

            //demo_fluentpath.demo();

            //demo_dapper.demo();

            //demo_httpclient.demo();

            //demo_mybatis.demo();
            
            //string clientIp = "10.76.175.45, 112.97.30.1";
            //int location = clientIp.IndexOf(",");
            //if (location > 0)
            //{
            //    clientIp = clientIp.Substring(0, location);
            //}
            
            //StructureMapTest.demo();

            //demo_openxml.demo();

            //DateTime dt09151200 = new DateTime(2014, 9, 15, 12, 0, 0);
            //DateTime dt09192400 = new DateTime(2014, 9, 19, 23, 59, 59);
            //DateTime now = new DateTime(2014, 9, 17, 12, 0, 0);
            //if (now >= dt09151200 && now <= dt09192400)
            //{

            //}

            //GiveDTReturnExcel();

            //RegexFound();

            //demo_redis.demo();

            //demo_quartz.demo();


            //long lLeft = 621355968000000000;
            //DateTime dt = DateTime.Now;
            //long Sticks = (dt.Ticks - lLeft) / 10000;
            //Console.WriteLine(Sticks);
            //string dtc = dt.ToString("yyyyMMddHHmmss");
            //string mm = dtc.Substring(8, 2);
            //string ss = dtc.Substring(12, 2);
            //long itemid = 1234789123;
            //long itemida = Convert.ToInt64((itemid.ToString() + ss));
            //long aitemid = Convert.ToInt64(itemida.ToString().Substring(0, itemida.ToString().Length - 2));

            //模拟js中的Math.Random
            //string a = "0." + new Random().Next(100000, 999999) + new Random().Next(100000, 999999) + new Random().Next(1000, 9999);

            //string path = System.AppDomain.CurrentDomain.BaseDirectory + @"temp\fuck\abcd\de\sde\gcd";
            //if (!System.IO.Directory.Exists(path))
            //{
            //    System.IO.Directory.CreateDirectory(path);
            //}

            //TcpClient tcpClient = new TcpClient();
            //tcpClient.ConnectAsync("127.0.0.1", 10000).ContinueWith(x =>
            //{
            //    NetworkStream ns = tcpClient.GetStream();
            //    Byte[] sendBytes = Encoding.UTF8.GetBytes("hello,from c#");               
            //    ns.Write(sendBytes, 0, sendBytes.Length);

            //    StreamReader reader = new StreamReader(ns);
            //    string received = reader.ReadToEnd();
            //    Console.WriteLine(received);
            //});     

            //var md5Value = Security.MD5Provider.Encrypt("hello=fuck&company=abcd");
            //var aesValue = Security.AesProvider.Encrypt("test");
            //var aes = Security.AesProvider.Decrypt("KJ6hza452Kec4LjLbfxvxw==");

            //demo_httpclient.demo();

            //demo_topshelf.demo();          

            //demo_retry.demo();

            //demo_drawpicture.demo();

            var client = new HttpClient();

            //var picurl = "http://pic.51zhangdan.com/2015/4/H6/sk/ifQ7WttvOjP3UVziL2rPLp12qQcKrLMH.png";
            //HttpResponseMessage response = client.GetAsync(picurl).Result;
            //HttpContent content = response.Content;            

            var posturl = "https://api.projectoxford.ai/face/v0/detections?subscription-key=c29cb8dfe7dd47d8abe7fbc35f5f58b9";

            var filestream = System.IO.File.Open("22.jpg", System.IO.FileMode.Open);
            StreamContent content = new StreamContent(filestream);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            var postresponse = client.PostAsync(posturl, content).Result;
            var pc = postresponse.Content;
            var ps = pc.ReadAsStringAsync().Result;        
            Console.WriteLine(ps);          

            Console.WriteLine("please click enter to exit ...");
            Console.ReadLine();
        }

        private static void RegexFound()
        {
            string regwanttofind = @"Is the cost of of gasline going up up? 
                                    Look up of the TV, your mobile phone is there. 
                                    You are the best of the the best. ";
            string regtext = @"(\w+) \1";
            Regex reg = new Regex(regtext);
            MatchCollection matchs = reg.Matches(regwanttofind);
            foreach (Match match in matchs)
            {
                string mv = match.Value;
            }

            regwanttofind = @"<h1>This is a valid header</h1> 
                            <h2>This is not valid.</h3> ";
            regtext = @"<h1>(?<h1value>.*?)</h1> ";
            reg = new Regex(regtext);
            string afterreplace = reg.Replace(regwanttofind, @"<h1 style=""background:#ff0"">${h1value}</h1>");

            regwanttofind = @"(020)82514769 
                                (021)83281314 
                                (029)88401132";
            regtext = @"\((?<area>\d{3})\)(?<number>\d{8})";
            reg = new Regex(regtext);
            afterreplace = reg.Replace(regwanttofind, "${area}-${number}");
        }

        /// <summary>
        /// 根据DataTable生成Excel文件
        /// </summary>
        private static void GiveDTReturnExcel()
        {
            string excelpath = System.AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.AddDays(-1).ToString("yyyyMMdd") + ".xlsx";
            DataTable dat = new DataTable();
            dat.Columns.Add("no");
            dat.Columns.Add("media");
            dat.Columns.Add("pcormobile");
            dat.Columns.Add("name");
            dat.Columns.Add("age");
            dat.Columns.Add("mobile");
            dat.Columns.Add("company");
            dat.Columns.Add("occuption");
            dat.Columns.Add("companyaddress");
            dat.Columns.Add("education");
            dat.Columns.Add("hascard");
            dat.Columns.Add("contactway");
            dat.Columns.Add("hasconfirm");
            DataRow dr = null;
            for (int i = 1; i < 6; i++)
            {
                dr = dat.NewRow();
                dr["no"] = i;
                dr["media"] = "51信用卡管家";
                if (i % 2 == 0)
                {
                    dr["pcormobile"] = "PC端";
                }
                else
                {
                    dr["pcormobile"] = "手机端";
                }
                dr["name"] = "media";
                dr["age"] = "media";
                dr["mobile"] = "media";
                dr["company"] = "media";
                dr["occuption"] = "media";
                dr["companyaddress"] = "media";
                dr["education"] = "media";
                dr["hascard"] = "media";
                dr["contactway"] = "media";
                dr["hasconfirm"] = "是";
                dat.Rows.Add(dr);
            }

            Openxml.OpenXml.GenerateExcelWithTemplate(dat, "guangfa.xlsx", excelpath);
            using (var zip = new Ionic.Zip.ZipFile())
            {
                zip.Password = "51@zhangdan";
                zip.AddFile(excelpath, "");
                zip.Save(System.AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.AddDays(-1).ToString("yyyyMMdd") + "广发日.zip");
            }
        }
    }
}